package com.ych.modules.cms.authorInfc.fileAuth.controller;

import com.ych.SDK.alibaba.ossSDK.AliOssUtil;
import com.ych.SDK.alibaba.ossSDK.config.AliOssConfig;
import com.ych.redis.RedisUtil;
import com.ych.redis.config.RedisKeyConfig;
import com.ych.utils.TLMap;
import com.ych.utils.UUID;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/19
 * Time: 15:21
 * Description:
 */
@Api(tags = {"CMS--文件上传相关"}, description = "CMS--文件上传相关")
@RestController
@RequestMapping("/cms/auth/fileAuth")
public class FileAuthController {
    @Autowired
    private              RedisUtil        redisUtil;
    @Autowired
    private              AliOssUtil       aliOssUtil;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    @ApiOperation(value = "获取文件上传地址", notes = "授权地址", position = 1)
    @PostMapping("/getUploadUrl")
    public R<String> getUploadUrl(@RequestBody String fileName) {
        try {
            String userId = TLMap.getUserId();//可空
            String suffix = fileName.substring(fileName.lastIndexOf("."));//后缀名
            String newFileName = UUID.randomUUID() + suffix; //重新用uuid命名
            String path;
            if (StringUtils.isBlank(userId)) {
                path = AliOssConfig.path + "/public/" + sdf.format(new Date()) + "/" + newFileName;
            } else {
                path = AliOssConfig.path + "/private/" + userId + "/" + sdf.format(new Date()) + "/" + newFileName;
            }
            String url = aliOssUtil.getOssObjectUploadAuthUrl(path);
            String key = RedisKeyConfig.fileTokenKey + path;
            redisUtil.redisTemplate().opsForValue().set(key, key);
            redisUtil.redisTemplate().expire(key, 3600, TimeUnit.SECONDS);//30分钟
            return new R(0, "授权成功", url);
        } catch (RuntimeException e) {
            return new R(1, e.getMessage(), null);
        } catch (Exception e) {
            return new R(2, "系统未知异常" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "* 马上获取文件下载路径", notes = "条件:首先调用过上传授权接口", position = 2)
    @PostMapping("/getDownloadUrl")
    @ApiImplicitParam(name = "path", value = "绝对路径", required = true, dataType = "String")
    public R<String> getFileUrl(@RequestBody String path) {
        path = aliOssUtil.getRelativePath(path);//转换成相对路径
        String key = RedisKeyConfig.fileTokenKey + path;
        try {
            String one = (String) redisUtil.redisTemplate().opsForValue().get(key);
            if (StringUtils.isBlank(one)) {
                throw new RuntimeException("未授权,获取下载连接失败");
            }
            String url = aliOssUtil.getOssObjectDownAuthUrl(path);
            if (StringUtils.isBlank(url)) {
                throw new RuntimeException("地址获取失败");
            }
            redisUtil.redisTemplate().delete(key);
            return new R(0, "地址获取成功", url);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
